#pragma once

#include <QSqlDatabase>
#include <QString>
#include <QDebug>
#include <QSqlQuery>

struct db_wrapper
{
    db_wrapper(const QString &driver_name, const QString &db_host_name, const QString &db_user_name, const QString &db_passwd, const QString &db_name)
    {
        const QString conn_name = db_host_name + "_" + db_name;

        if (QSqlDatabase::contains(conn_name))
        {
            db_ = QSqlDatabase::database(conn_name);
        }
        else
        {
            db_ = QSqlDatabase::addDatabase(driver_name, conn_name);
            db_.setDatabaseName(db_name     );
            db_.setHostName    (db_host_name);
            db_.setUserName    (db_user_name);
            db_.setPassword    (db_passwd   );

            if (!db_.open())
                qWarning() << "Can't connect to destination database " << db_name << " driver name " << driver_name << " host name " << db_host_name << " user name " << db_user_name;
        }
    }

    QSqlQuery getQuery() const
    {
        return QSqlQuery(db_);
    }

    QString dbName() const
    {
        return db_.databaseName();
    }

    bool isOpen() const
    {
        return db_.isOpen();
    }

private:
    QSqlDatabase db_;
};
